package jusda.caa.minio.controller;

import io.swagger.annotations.*;
import jusda.caa.minio.config.BaseResult;
import jusda.caa.minio.config.CustomException;
import jusda.caa.minio.entity.FileAttributeDTO;
import jusda.caa.minio.entity.UploadResultDTO;
import jusda.caa.minio.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/files")
@Api("文件操作控制器")
public class FileController {
    @Autowired
    FileService fileService;

    @PostMapping("/upload")
    @ApiOperation(value="文件上传", notes="文件上传接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "需要上传的文件", required = true, paramType = "body", dataType = "MultipartFile"),
            @ApiImplicitParam(name = "bucketName", value = "文件桶名称", required = true, paramType = "body", dataType = "String"),
            @ApiImplicitParam(name = "fileName", value = "文件名称 不带后缀", required = true, paramType = "body", dataType = "String"),
            @ApiImplicitParam(name = "description", value = "文件描述", paramType = "body", dataType = "String"),
            @ApiImplicitParam(name = "fileSuffix", value = "文件后缀名 不带.", required = true, paramType = "body", dataType = "String")
    })
    public BaseResult<UploadResultDTO> uploadFile(@RequestBody MultipartFile file, String bucketName, String fileName, String description, String fileSuffix) throws IOException, CustomException {
        return new BaseResult(fileService.uploadFile(bucketName,file.getInputStream(),fileName,description,fileSuffix),1);
    }

    @ApiOperation(value="文件获取", notes="获取文件访问的临时url 五分钟之内有效")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bucketName", value = "文件桶名称", required = true, paramType = "path", dataType = "String"),
            @ApiImplicitParam(name = "fileName", value = "文件服务器返回的名称", required = true, paramType = "path", dataType = "String"),
    })
    @GetMapping("/get/{bucketName}/{fileName:.+}")
    public BaseResult<String> get(@PathVariable String bucketName,@PathVariable String fileName) throws CustomException {
        String fileURL = fileService.getFileURL(bucketName, fileName);
        return new BaseResult(fileURL,1);
    }

    @ApiOperation(value="文件流获取", notes="获取文件流下载")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bucketName", value = "文件桶名称", required = true, paramType = "path", dataType = "String"),
            @ApiImplicitParam(name = "fileName", value = "文件服务器返回的名称", required = true, paramType = "path", dataType = "String"),
    })
    @GetMapping("/getStream/{bucketName}/{fileName:.+}")
    public BaseResult getStream(@PathVariable String bucketName,@PathVariable String fileName, HttpServletResponse response) throws IOException, CustomException {
        fileService.getFile(bucketName, fileName,response);
        return new BaseResult("OK",1);
    }

    @ApiOperation(value="删除文件", notes="删除指定文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bucketName", value = "文件桶名称", required = true, paramType = "path", dataType = "String"),
            @ApiImplicitParam(name = "fileName", value = "文件服务器返回的名称", required = true, paramType = "path", dataType = "String"),
    })
    @GetMapping("/delete/{bucketName}/{fileName:.+}")
    public BaseResult delete(@PathVariable String bucketName,@PathVariable String fileName) throws CustomException {
        fileService.deleteFile(bucketName, fileName);
        return new BaseResult("ok",1);
    }

    @ApiOperation(value="获取文件详细", notes="获取文件服务器内部的文件属性详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bucketName", value = "文件桶名称", required = true, paramType = "path", dataType = "String"),
            @ApiImplicitParam(name = "fileName", value = "文件服务器返回的名称", required = true, paramType = "path", dataType = "String"),
    })
    @GetMapping("/detail/{bucketName}/{fileName:.+}")
    public BaseResult<FileAttributeDTO> detail(@PathVariable String bucketName, @PathVariable String fileName) throws CustomException {
        return new BaseResult(fileService.fileDetail(bucketName, fileName),1);
    }
}
